﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Admin_jiaju.Application.Students;
using Admin_jiaju.Application.Students.Dtos;
using Admin_jiaju.Core.Models;
using Admin_jiaju.Core.Repositories;
using Admin_jiaju.mvc.Controllers;
using Admin_jiaju.mvc.Models.Students;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Admin_jiaju.mvc.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class BannerController : BaseController
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IRepository<Banner, int> _repository;
        private readonly IAdmin_jiajuService _admin_JiajuService;
        private readonly IMapper _mapper;

        public BannerController(ILogger<HomeController> logger,
             IRepository<Banner, int> repository,
             IAdmin_jiajuService admin_JiajuService,
             IMapper mapper

        )
        {
            _logger = logger;
            _repository = repository;
            _admin_JiajuService = admin_JiajuService;
            _mapper = mapper;
        }
        [HttpGet]
        public async Task<IActionResult> List(StudnetInputDto input)
        {
            // var student = await _repository.GetAllListAsync(input);
            var student = await _admin_JiajuService.GetPagedResultAsync(input);
            //return Json(student);
            return Json(ListResult((student.Data), student.TotalCount));
        }
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult Add()
        {
            return View();
        }
        
        [HttpPost]
        public async  Task<IActionResult> Add(BannerView bannerView)
        {
            await  _repository.InsertAsync(_mapper.Map<Banner>(bannerView));
          bool a= await _repository.SavesAsync();
            if (a == true)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }
        }
        public async  Task<IActionResult> Edit(int Id)
        {
            //通过Id获取数据详情
            var bannerDel =await _repository.FirstOrDefaultAsync(t => t.Id == Id);
            //把详细地址传到页面
            return View(_mapper.Map<BannerView>(bannerDel));
        }
        [HttpPost]
        public async Task<IActionResult> Edit(BannerView bannerView)
        {
            var student=await _repository.UpdateAsync(_mapper.Map<Banner>(bannerView));
            //编辑，请求数据库       
            if (student == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(AjaxResult(true));
            //bool a = await _repository.SavesAsync();
            //if (a == true)
            //{
            //    return Json(new { success = true, msg = "编辑成功" });
            //}
            //else
            //{
            //    return Json(new { success = false, msg = "编辑失败" });
            //}
            //bool a = await _repository.SavesAsync();
            //if (a==true)
            //{
            //    return Json(new { success = true, msg = "编辑成功" });
            //}
            //else
            //{
            //    return Json(new { success = false, msg = "编辑失败" });
            //}
        }
        [HttpPost]
        public async Task<IActionResult> DelArt(int Id)
        {
           var a= await _repository.FirstOrDefaultAsync(t=>t.Id==Id);
            if (a == null )
            {
                return Json(new { success = false, msg = "该用户不存在无法删除" });
            }
            await _repository.DeleteAsync(t => t.Id == Id);
            bool n = await _repository.SavesAsync();
            if (n == true)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }
            

        }
    }
}
